<?php
/**
* Simple.php
*
* Contains Simple class definition
* @author  John Snook <jsnook@gmail.com>
* @version 2
* @copyright (c) Snook 2004
* @package QuickDataFiles
*/

/**
* Simple
*
* This class is the base class of all  other QDF edit controls.  It's most basic use is a bound data control for a textbox to a field.
* @author  John Snook <jsnook@gmail.com>
* @version 2
* @copyright (c) Snook 2004
* @example /var/www/corp/IntraDox/Documents.php
* @todo Convert to PEAR::DB, break out superclass from simple text class.
* @package QuickData
* @subpackage QuickDataFields
*/
class Simple extends Field
{
	var $DataType = "varchar";
	var $Size = 50;
	var $Format;
	
	function Simple($Properties) //expects $Caption, $LocalField, $DataType = "varchar", $Size = 50
	{
		parent::Field($Properties);
	}

	function GetSQLReadyValue()
	{
		if (is_null($this->Value))
		{
			$SQL .= "null";
		}
		else
		{
			switch ($this->DataType)
			{
				case "time":
				case "date":
				case "varchar":
				case "password":
				case "text":
					$SQL .= "'" . trim($this->Value) . "'";
					break;
				case "number":
					$SQL .= $this->Value;
					break;
			}
		}
		return $SQL;
	}
	
	function Update()
	{
		$DB = $this->Parent->Parent->DB;
		$SQL = "UPDATE " . $this->Parent->Parent->DBTableName . " set $this->LocalField = " . $this->GetSQLReadyValue();
		$SQL .= " WHERE " . $this->Parent->Parent->PrimaryKeyName . " = " . $this->Parent->PrimaryKeyValue;
		$Query = $DB->CreateQuery($SQL);
		$Query->Close(); 
	}
	
	function Render($DataRow, $DisplayMode = QD_MODE_VIEW)
	{
		if ($DisplayMode != QD_MODE_ADD)
		{
			$this->Value = $DataRow[$this->LocalField];
		}
		else
		{
			$this->Value = $this->DefaultValue;
		}
		
		if ($this->Visible)
		{
			$out .= "\t<tr>\n\t\t<td class=\"" . $this->Parent->ViewCaptionCssClassName . "\">$this->Caption</td>\n";
			$out .= "\t\t<td class=\"" . $this->Parent->ViewCssClassName . "\">";
			if ( ($DisplayMode == QD_MODE_EDIT && $this->CanEdit) ||
				($DisplayMode == QD_MODE_ADD && $this->CanAdd) || 
				($DisplayMode == QD_MODE_SEARCH && $this->CanSearch) 
			   )
			{
				switch ($this->DataType)
				{
					case "varchar":
						$size = ($this->Size > 50) ?  50 : $this->Size;
						$maxlength = $this->Size;
						$out .= "<input name=\"$this->LocalField\"  size=\"$size\" maxlength=\"$maxlength\" class=\"" . $this->Parent->EditCssClassName . "\" type=\"text\" value=\"" . $this->Value . "\">";
						break;
					case "password":
						$size = ($this->Size > 50) ?  50 : $this->Size;
						$maxlength = $this->Size;
						$out .= "<input name=\"$this->LocalField\"  size=\"$size\" maxlength=\"$maxlength\" class=\"" . $this->Parent->EditCssClassName . "\" type=\"password\" value=\"" . $this->Value . "\">";
						break;
					case "text":
						$out .= "<textarea name=\"$this->LocalField\" class=\"" . $this->Parent->EditCssClassName . "\" rows=\"4\" cols=\"80\">" . $this->Value . "</textarea> ";
						break;
					case "number":
						$out .= "<input name=\"$this->LocalField\" size=\"10\" maxlength=\"10\" class=\"" . $this->Parent->EditCssClassName . "\" type=\"text\" value=\"" . $this->Value . "\">";
						break;
					case "date":
						if($DisplayMode == QD_MODE_SEARCH)
						{
							$out .= "Between ";
						}
						$out .= "<input name=\"$this->LocalField\" size=\"10\" maxlength=\"10\" class=\"" . $this->Parent->EditCssClassName . "\" type=\"text\" value=\"" . $this->Value . "\">";
						$out .= "&nbsp;<a href=\"#\" onClick=\"show_calendar('QuickData.$this->LocalField');\" onMouseOver=\"window.status='Date Picker'; overlib('Click here to choose a date'); return true;\" onMouseOut=\"window.status=''; nd(); return true;\">";
						$out .= "<img align=\"absmiddle\" src=\"{$this->Parent->Parent->QuickDataPath}/images/calendar.gif\"></a>\n";
						if($DisplayMode == QD_MODE_SEARCH)
						{
							$out .= " AND <input name=\"{$this->LocalField}2\" size=\"10\" maxlength=\"10\" class=\"" . $this->Parent->EditCssClassName . "\" type=\"text\" value=\"" . $this->Value . "\">";
							$out .= "&nbsp;<a href=\"#\" onClick=\"show_calendar('QuickData.{$this->LocalField}2');\" onMouseOver=\"window.status='Date Picker'; overlib('Click here to choose a date'); return true;\" onMouseOut=\"window.status=''; nd(); return true;\">";
							$out .= "<img align=\"absmiddle\" src=\"{$this->Parent->Parent->QuickDataPath}/images/calendar.gif\"></a>\n";
						}
						break;
					default:
						$out .= $this->Value . " Unknown FieldType type = $this->DataType, Size = $this->Size";
						break;
				}
			}
			else
			{
				if ($this->DataType != "password")
				{
					$out .= $this->Value;
					$out .= "<input type=\"hidden\" name=\"{$this->LocalField}\" value=\"" . addslashes($this->Value) . "\">";
				}
				else
				{
					$len = strlen($this->Value);
					$out .= str_repeat("*", $len);
				}
			}
			$out .= "</td>\n\t</tr>";
			return $out;

		}
	} // if visible
}

?>